package com.kystar.muxplayer.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.kystar.muxplayer.R;
import com.kystar.muxplayer.model.Settings;

/**
 * Created by zhusheng on 2018-03-10.
 */
public class GroupView implements Collapse {
    public View mGroupView;
    public ImageView mArrowView;
    public View mTitleView;
    public ViewGroup mContentView;
  //  public Connection connection;
    Collapse lastSubGroup;

    public GroupView(Context context, String name) {
        LayoutInflater inflater = LayoutInflater.from(context);
        mGroupView = inflater.inflate(R.layout.layout_main_settings_group_panel, null);
        mTitleView = mGroupView.findViewById(R.id.settings_group_title);
        mArrowView = mGroupView.findViewById(R.id.settings_group_title_arrow);
        mContentView = mGroupView.findViewById(R.id.settings_group_content);
        ((TextView) mGroupView.findViewById(R.id.settings_group_name)).setText(name);
    }

    public void addSubGroup(String title, int contentID, boolean last) {
        addSubGroup(new SubGroupView(mGroupView.getContext(), title, contentID, last));
    }

    public void addSubGroup(SubGroupView subGroup) {
        mContentView.addView(subGroup.root);
        subGroup.title.setOnClickListener(v -> {
            if (lastSubGroup == subGroup) {
                subGroup.collapse(true);
                return;
            }

            if (lastSubGroup != null) {
                lastSubGroup.collapse(false);
            }
            subGroup.collapse(true);
            lastSubGroup = subGroup;
        });
    }

    public void setOnTitleClick(View.OnClickListener listener) {
        mTitleView.setOnClickListener(listener);
    }

    public Context getContext() {
        return mContentView.getContext();
    }

    public static View setupOkCancel(View root, Runnable ok, Runnable cancel) {
        root.findViewById(R.id.button_ok).setOnClickListener(v -> ok.run());
        root.findViewById(R.id.button_cancel).setOnClickListener(v->cancel.run());
        return root;
    }

    public static View setupConfirm(View root, String confirm, Runnable ok, Runnable cancel) {
        ((TextView)root.findViewById(R.id.confirm_text)).setText(confirm);
        setupOkCancel(root.findViewById(R.id.confirm_buttons), ok, cancel);
        return root;
    }

    public  <T extends View> T findViewById(int resId) {
        return mContentView.findViewById(resId);
    }

    @Override
    public void collapse(boolean toggle) {
        if (toggle && mContentView.getVisibility() != View.VISIBLE) {
            mArrowView.setImageResource(R.drawable.settings_title_up);
            mContentView.setVisibility(View.VISIBLE);
        } else {
            if(lastSubGroup != null) {
                lastSubGroup.collapse(false);
                lastSubGroup = null;
            }
            mArrowView.setImageResource(R.drawable.settings_title_down);
            mContentView.setVisibility(View.GONE);
        }
    }
}
